home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dirut / delupto.zip / DELUPTO.C next >
C/C++ Source or Header  |  1992-09-28  |  4KB  |  124 lines

  1. /************************************************************************
  2.  *   DelUpto   ---  Deletes files with a date equal or prior to a given *
  3.  *                  argument.                                           *
  4.  *                                                                      *
  5.  *   Written By:    Steve Palm                                          *
  6.  *   Date:          09-28-92                                            *
  7.  *   Written for:   Anyone who wants to benefit from it.                *
  8.  *                                                                      *
  9.  *   Bugs:          None                                                *
  10.  *                                                                      *
  11.  ************************************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <dos.h>
  15. #include <dir.h>
  16. #include <sys\stat.h>
  17. #include <ctype.h>
  18. #include <time.h>
  19.  
  20. int main (int argc, char *argv[])
  21. {
  22.     int month, day, year, done;
  23.     char *pdate, *ppat, answer;
  24.     struct ffblk fblk;
  25.     struct stat  statbuf;
  26.     struct tm    *filetime;
  27.  
  28.     /* Print out banner */
  29.  
  30.     printf("\nDelUpto v1.0 --- Written by Steve Palm\n\n");
  31.  
  32.     /* Make sure the user gives us a date to work with.  If not, print out */
  33.     /* some instructions for them. */
  34.  
  35.     if (argc < 3) {
  36.         printf("USAGE:  DelUpto <date> <pattern>\n\n");
  37.         printf("This utility Deletes specified files with a date equal to or earlier than\n");
  38.         printf("the date specified (mm/dd/yy, mm-dd-yy, mm/dd/yyyy, or mm-dd-yyyy).\n\n");
  39.         exit(1);
  40.     }
  41.  
  42.     /* Okay.  Ready to do some work.  Set up working pointers. */
  43.  
  44.     pdate = argv[1];
  45.     ppat  = argv[2];
  46.  
  47.     /* parse date handed to us.                                         */
  48.     /* I'm not checking the date for validity, because the date is only */
  49.     /* being used for comparison, so values larger than reality will    */
  50.     /* have the expected effect of deleting all possible values.        */
  51.     /* I do check to see that all parts are there, however.             */
  52.  
  53.     month = atoi(pdate);          /* Grab number portion */
  54.     while (isdigit(*pdate))       /* parse up to separator, */
  55.         pdate++;                  /* skipping digits on the way */
  56.     pdate++;                      /* finally skip past the separator */
  57.  
  58.     day   = atoi(pdate);          /* same as above */
  59.     while (isdigit(*pdate))
  60.         pdate++;
  61.     pdate++;
  62.  
  63.     year = atoi(pdate);           /* year is just the remainder */
  64.  
  65.     /* Handle years > 2000 by saying anyhing LESS THAN 70 (1970) */
  66.     /* is actually 2070, not 1970.  MS-DOS cannot have a date    */
  67.     /* before 1970, so this is a safe operation.                 */
  68.  
  69.     if (year < 70)
  70.         year += 100;
  71.     else if (year > 1900)
  72.         year -= 1900;
  73.  
  74.     /* re-set date pointer */
  75.     pdate = argv[1];
  76.  
  77.     if ((month == 0) || (day == 0) || (year == 0)) {
  78.         printf ("Invalid date: %s\n\n", pdate);
  79.         exit(1);
  80.     }
  81.  
  82.     /* Tell the user what the parameters are.  (They told US) */
  83.     printf ("Deleting files matching %s, dated on or before: %s\n\n", ppat, pdate);
  84.  
  85.     /* ask if they want to do this. Confirmation. */
  86.     printf ("Continue (y/n)? ");
  87.     answer = getche();
  88.     printf("\n");                           /* output a newline to look nice. */
  89.  
  90.     if ((answer != 'y') && (answer != 'Y'))
  91.         exit(0);
  92.  
  93.     /* Look for first file */
  94.     done = findfirst(ppat, &fblk, 0);
  95.  
  96.     /* Loop until we have no more files */
  97.     while (!done) {
  98.         /* get file time, and convert to tm strcture */
  99.         (void) stat(fblk.ff_name, &statbuf);
  100.         filetime = localtime(&statbuf.st_atime);
  101.  
  102.         /* filetime->tm_mon is 0-11, we want 1-12 */
  103.         filetime->tm_mon += 1;
  104.  
  105.         /* check to see if this file gets nuked */
  106.         if ((filetime->tm_year < year) ||
  107.            ((filetime->tm_year == year) && (filetime->tm_mon < month)) ||
  108.            (((filetime->tm_year == year) && (filetime->tm_mon == month) &&
  109.              (filetime->tm_mday <= day)))) {
  110.  
  111. /*                      printf ("NUKING: %s, dated %d-%d-%d\n", fblk.ff_name,
  112.                         filetime->tm_mon,
  113.                         filetime->tm_mday,
  114.                         (filetime->tm_year + 1900));
  115. */
  116.             remove(fblk.ff_name);
  117.         }
  118.         /* get next file */
  119.         done = findnext(&fblk);
  120.     }
  121.     /* finished.  Get out of here! */
  122.     exit (0);
  123. }
  124.